var api = require('../../../api.js');
var utils = require('../../../utils.js');
// var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();


// pages/integral/integral.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: "",
        // 点击签到 弹框
        sign_show: true,
        // 积分规则
        rules_show: true,
        // 兑换券遮罩
        ticket_show: true,

    },
    // 积分兑换券
    haveticket: function(e) {
        var that = this;
        // 优惠券id
        let id = e.currentTarget.dataset.id;
        app.request({
            url: api.integralmall.exchange_coupon,
            data: {
                id: id
            },
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
                console.log(res)
                console.log(res.code)
                console.log("==========================")
                // 成功的话显示 遮罩层
                that.setData({
                    ticket_show: !that.data.ticket_show,
                    ticket_code: res.code
                })
                let title = res.msg
                if (res.code == 0) {
                    that.setData({
                        title: title
                    })
                }
                if (res.code == 1) {
                    that.setData({
                        title: title
                    })
                }
                if (res.code == 2) {
                    that.setData({
                        title: title
                    })
                }
                if (res.code == 3) {
                    that.setData({
                        title: title
                    })
                }
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    // 积分规则
    rules: function() {
        var that = this
        that.setData({
            rules_show: !that.data.rules_show
        })
    },
    // 点击跳转兑换券 全部
    tocoupons: function() {
        wx.navigateTo({
            url: '/page/integral/pages/coupons/coupons',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    // 签到弹框 关闭
    close_click_sign:function(){
        var that = this;
        that.setData({
            sign_show: true,
        })
    },
    // 点击签到
    click_sign: function() {
        var that = this
        that.setData({
            sign_show: !that.data.sign_show
        })
        // 获取当前日期
        var now_day = new Date();
        var mydate = now_day.getFullYear() + '-' + (now_day.getMonth() + 1) + '-' + now_day.getDate()
        // console.log(mydate + "==系统日期")
    

        app.request({
            url: api.integralmall.register,
            data: {
                today: mydate,
            },
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
                console.log(res)
                that.setData({
                    res:res,
                    sign_title: res.msg,
                    sign_code: res.code,
                    // 连续签到时间
                    // continuous: res.data.continuation
                })
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    //
    swiperChange: function(e) {
        this.setData({
            swiperCurrent: e.detail.current //获取当前轮播图片的下标
        })
    },
    // 跳转more 更多商品页面
    tomore: function(e) {
        console.log(e.currentTarget.dataset.id)

        console.log("++++++++++++++++++more")
        var listid = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/page/integral/pages/shoplist/shoplist?id=' + listid,
        })
    },
    // 跳转 兑换记录
    torecord: function() {
        var that = this;
        var u_jf = that.data._data.user.integral;
        wx.navigateTo({
            url: '/page/integral/pages/record/record?u_jf=' + u_jf,
            success: function(res) {},
        })
    },

    // 弹出详情 弹框
    detail: function(e) {
        var that = this
        console.log(e.currentTarget.dataset)
        console.log(e.currentTarget.dataset.indexs)
        var cat_id = e.currentTarget.dataset.cat_id - 1
        var id_index = e.currentTarget.dataset.indexs

        // console.log(that.data._data.goods_list)

        var id = that.data._data.goods_list[cat_id].goods[id_index].id;
        // 用户积分
        var u_jf = that.data._data.user.integral

        wx.navigateTo({
            url: '/page/integral/pages/shopdetail/shopdetail?cat_id=' + cat_id + '&id_index=' + id_index + '&id=' + id + '&u_jf=' + u_jf,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })

    },
    // 图片关闭弹框
    closedetail: function() {
        var that = this
        that.setData({
            detail_show: true
        })
    },
    // 防止遮罩层穿透
    myCatchTouch: function() {
        console.log('stop user scroll it!');
        return;
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options, e) {
        var that = this
        //   禾匠 積分商城 data
        app.request({
            url: api.integralmall.index,
            data: '',
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
                that.setData({
                    _data: res.data,
                })
            },
            fail: function(res) {},
            complete: function(res) {
                app.request({
                    url: api.integralmall.exchange,
                    data: '',
                    header: {},
                    method: 'GET',
                    dataType: 'json',
                    responseType: 'text',
                    success: function(res) {
                        that.setData({
                            user_img: res.data.list[0].avatar_url
                        })
                    },
                    fail: function(res) {},
                    complete: function(res) {},
                })
            },
        })
        // 获取设备信息
        wx.getSystemInfo({
            success: function(res) {
                console.log(res.windowHeight)
                console.log("=============================")
                that.setData({
                    winh: res.windowHeight
                })
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function(e) {
        var that = this
        //   禾匠 積分商城 data
        app.request({
            url: api.integralmall.explain,
            data: '',
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
                console.log(res.data)
                console.log("+++++++++++++++++++++++++++")
                that.setData({
                    _title: res.data.setting.integral_shuoming[0].title,
                    _content: res.data.setting.integral_shuoming[0].content,
                    _register_rule: res.data.setting.register_rule,
                    // 连续签到的天数
                    _days: res.data.setting.register_continuation,
                    // 每日获得的积分数
                    _integral: res.data.setting.register_integral,
                    // 连续签到的赠送积分
                    _reward: res.data.setting.register_reward,

                })
                wx.setNavigationBarTitle({
                    title: that.data._title,
                });

            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})